﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace HttpClientPoxy
{
    public class RsaComputer
    {
        //RSACryptoServiceProvider rsaDecoder = null;
        RSACryptoServiceProvider rsaEncoder = null;

        static RsaComputer rsa;
        public static RsaComputer GetComputer()
        {
            if (rsa == null)
            {
                rsa = new RsaComputer();
                rsa.rsaEncoder = new RSACryptoServiceProvider();
                rsa.rsaEncoder.FromXmlString(File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"\pem\publickey.xml"));
                //rsa.rsaDecoder = new RSACryptoServiceProvider();
                //rsa.rsaDecoder.FromXmlString(File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"\pem\privatekey.xml"));
            }
            return rsa;
        }

        public string Encode(string data)
        {
            try
            {
                return Convert.ToBase64String(rsaEncoder.Encrypt(Encoding.UTF8.GetBytes(data), false));
            }
            catch
            {
                return string.Empty;
            }
        }

        //public string Decode(string data)
        //{
        //    try
        //    {
        //        return Encoding.UTF8.GetString(rsaDecoder.Decrypt(Convert.FromBase64String(data), false));
        //    }
        //    catch
        //    {
        //        return string.Empty;
        //    }
        //}

    }
}
